package com.example.academic_affairs_system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.academic_affairs_system.common.RPage;
import com.example.academic_affairs_system.exception.SystemException;
import com.example.academic_affairs_system.mapper.AasCoursesMapper;
import com.example.academic_affairs_system.model.dto.classCourses.ClassCoursesDTO;
import com.example.academic_affairs_system.model.dto.classCourses.ClassCoursesPageDTO;
import com.example.academic_affairs_system.model.entity.AasClass;
import com.example.academic_affairs_system.model.entity.AasCourses;
import com.example.academic_affairs_system.model.vo.LoginResponseVO;
import com.example.academic_affairs_system.model.vo.courses.CoursesVO;
import com.example.academic_affairs_system.service.IAasClassCoursesService;
import com.example.academic_affairs_system.service.IAasClassService;
import com.example.academic_affairs_system.service.IAasCoursesService;
import com.example.academic_affairs_system.utils.TokenUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 课程表 服务实现类
 *
 * @author hjx
 * @since 2023-01-31
 */
@Service
public class AasCoursesServiceImpl extends ServiceImpl<AasCoursesMapper, AasCourses>
    implements IAasCoursesService {
  @Resource IAasClassCoursesService iAasClassCoursesService;
  @Resource IAasClassService iAasClassService;
  @Resource HttpServletRequest request;

  @Override
  @Transactional
  public long createCourses(ClassCoursesDTO dto) {
    // 创建课程
    LoginResponseVO loginResponseVO = TokenUtils.parseToken(request);
    AasCourses aasCourses = dto.buildCourses();
    aasCourses.setDirectorUserId(loginResponseVO.getUserId());
    baseMapper.insert(aasCourses);
    // 追加课程与班级的关联
    if (dto.getClassId() != null) {
      List<AasClass> classByDirectorsId =
          iAasClassService.getClassByDirectorsId(loginResponseVO.getUserId());
      // 判断当前用户是否有权操作
      if (CollectionUtil.isNotEmpty(classByDirectorsId)
          && classByDirectorsId.stream().anyMatch(ac -> ac.getId().equals(dto.getClassId()))) {
        iAasClassCoursesService.addRelated(dto.getClassId(), aasCourses.getId());
      } else {
        throw new SystemException(-999, "无权限操作指定班级", null);
      }
    }
    return aasCourses.getId();
  }

  @Override
  public int edit(ClassCoursesDTO dto) {
    Long courseId = dto.getCoursesId();
    AasCourses aasCourses = baseMapper.selectById(courseId);
    if (aasCourses == null) {
      throw new SystemException(-999, "课程不存在", null);
    }
    int i = 0;
    // 更新信息
    if (dto.updateMsg()) {
      AasCourses dtoCourses = dto.buildCourses();
      dtoCourses.setId(aasCourses.getId());
      i += baseMapper.updateById(dtoCourses);
    }
    // 更新班级与课程的关联
    if (dto.updateRelated()) {
      if (iAasClassCoursesService.reRelated(dto.getClassId(), dto.getCoursesId())) {
        // 说明课程已经被绑定
        throw new SystemException(-999, "请勿重复绑定", "班级已经绑定了该课程");
      }
      i += iAasClassCoursesService.addRelated(dto.getClassId(), dto.getCoursesId());
    }
    return i;
  }

  @Override
  public RPage<CoursesVO> getCoursesVOPage(ClassCoursesPageDTO dto) {
    return RPage.<CoursesVO>createDto(dto)
        .execute(page -> baseMapper.selectCourseVOList(page))
        .build();
  }

  @Override
  public List<CoursesVO> getDirectorCourses() {
    LoginResponseVO loginResponseVO = TokenUtils.parseToken(request);
    return CoursesVO.buildByEntities(
        baseMapper.selectList(
            Wrappers.<AasCourses>lambdaQuery()
                .eq(AasCourses::getDirectorUserId, loginResponseVO.getUserId())));
  }
}
